package com.rabbitmq2;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * 作者: LCG
 * 日期: 2019/9/18 16:26
 * 描述:
 */
public class RabbitMqTest {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {

        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost("112.74.214.177");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("zhangsan");
        connectionFactory.setPassword("zhangsan");
        connectionFactory.setConnectionTimeout(5000);
        connectionFactory.setVirtualHost("/");

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare("exchenage-test",BuiltinExchangeType.DIRECT,true);
        //死信交换器
        channel.exchangeDeclare("dead-exchenage-test",BuiltinExchangeType.DIRECT,true);

        channel.queueDeclare("dead-test-queue",true,false,false,null);

        Map<String,Object> map=new HashMap<String,Object>();

        //map.put("x-message-ttl",10000);
        map.put("x-dead-letter-exchange","dead-exchenage-test");//绑定私信交换器
        map.put("x-dead-letter-routing-key","DEAD-KEY");//绑定死信路由键

        channel.queueDeclare("test-queue",true,false,false,map);



        channel.queueBind("test-queue","exchenage-test","test-key");

        //死信队列绑定路由键
        channel.queueBind("dead-test-queue","dead-exchenage-test","DEAD-KEY");

        //AMQP.BasicProperties.Builder build=new AMQP.BasicProperties().builder();
        //build.expiration("10000");
        //AMQP.BasicProperties basicProperties=build.build();
        //channel.basicPublish("exchenage-test","test-key",basicProperties,"hello,message !".getBytes());

        //channel.txSelect();//打开事务

        AMQP.Confirm.SelectOk selectOk = channel.confirmSelect();//打开消息发送确认

        channel.basicPublish("exchenage-test","test-key",null,"hello,message !".getBytes());

        //boolean waitForConfirms = channel.waitForConfirms();

        //channel.txCommit();//提交事务

        //
        //channel.txRollback();//回滚事务

        channel.close();

        connection.close();



    }

}
